public class GetIntersectionNode {
    // leetcode 相交链表
    // https://leetcode.cn/problems/intersection-of-two-linked-lists/description/?envType=study-plan-v2&envId=top-100-liked
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int len1 = 0, len2 = 0;
        ListNode cur1 = headA, cur2 = headB;
        while (cur1 != null) {
            len1++;
            cur1 = cur1.next;
        }
        while (cur2 != null) {
            len2++;
            cur2 = cur2.next;
        }
        // 保证cur1始终指向的是较长的链表
        if (len1 < len2) {
            cur1 = headB;
            cur2 = headA;
        }else {
            cur1= headA;
            cur2 = headB;
        }
        for (int i = 0; i < Math.abs(len1-len2); i++) {
            cur1 = cur1.next;
        }
        while (cur1 != null) {
            if (cur1 == cur2) return cur1;
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return null;
    }
}
